<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:dct="http://purl.org/dc/terms/"
		xmlns:ical="http://www.w3.org/2002/12/cal/ical#"
		xmlns:foaf="http://xmlns.com/foaf/spec/"
		xmlns:dclite4g="http://dclite4g.xmlns.com/schema.rdf#"
                xmlns:gmd="http://www.isotc211.org/2005/gmd"
                xmlns:gts="http://www.isotc211.org/2005/gts"
                xmlns:gco="http://www.isotc211.org/2005/gco"
                xmlns:gml="http://www.opengis.net/gml"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:owl="http://www.w3.org/2002/07/owl#"  
>
<xsl:output method="xml" indent="yes" />

  <!-- write out the RDF header and footer -->
  <xsl:template match="gmd:MD_Metadata">
  <rdf:RDF
       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

  <!-- what do we pick for a Resource identifier - first onlineResource? -->
  <dclite4g:DataSet>
    <xsl:if test="gmd:fileIdentifier">
    <xsl:attribute name="rdf:about">
      <xsl:value-of select="gmd:fileIdentifier" />
    </xsl:attribute>
    </xsl:if>

    <xsl:if test="gmd:fileIdentifier">
    <dc:identifier><xsl:value-of select="gmd:fileIdentifier"/></dc:identifier>
    </xsl:if>
        
    <xsl:apply-templates select="gmd:identificationInfo/gmd:MD_DataIdentification"/>
        
    <!-- contact details for data set -->         
    <dc:contact> 
      <xsl:apply-templates select="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact"/>
    </dc:contact>

    <!-- spatial extents -->
    <dct:spatial>
    <xsl:apply-templates select="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox"/>
    </dct:spatial>

    <!-- temporal extents -->
    <xsl:apply-templates select="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod"/>

   <xsl:apply-templates select="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation"/>

    <!-- metadata on metadata -->
    <!-- metadata contact details -->
    <gmd:contact>
    <xsl:apply-templates select="gmd:contact"/>
    </gmd:contact>

    <!-- file and service locations -->
    <xsl:apply-templates select="gmd:distributionInfo/gmd:MD_Distribution"/>
  </dclite4g:DataSet>

 
  </rdf:RDF>
  </xsl:template>

  <xsl:template match="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:pointOfContact">
	<!-- no identifying URL, just an email :/
        <xsl:attribute name="rdf:about">
	  <xsl:value-of select="something_useful" />
	</xsl:attribute>
        -->
        <xsl:apply-templates select="gmd:CI_ResponsibleParty"/> 
  </xsl:template>

  <xsl:template match="gmd:contact">
    <xsl:apply-templates select="gmd:CI_ResponsibleParty"/>
  </xsl:template>


  <xsl:template match="gmd:CI_ResponsibleParty">
    <xsl:choose>
    <xsl:when test="gmd:individualName/gco:CharacterString">
    <foaf:Person>
    <foaf:name><xsl:value-of select="gmd:individualName/gco:CharacterString"/></foaf:name>
    <xsl:apply-templates select="gmd:contactInfo"/>
    </foaf:Person>
    </xsl:when>
    <xsl:otherwise>
    <foaf:Organisation>
    <foaf:name><xsl:value-of select="gmd:organisationName/gco:CharacterString"/></foaf:name>
    <xsl:apply-templates select="gmd:contactInfo"/>
    </foaf:Organisation>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>

    <xsl:template match="gmd:contactInfo">    
              <xsl:if test="gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString">
            <foaf:mbox>                                                                           <xsl:value-of select="gmd:CI_Contact/gmd:address/gmd:CI_Address/gmd:electronicMailAddress/gco:CharacterString" />             </foaf:mbox>
          </xsl:if>                                                                         
    </xsl:template>


  <xsl:template match="gmd:identificationInfo/gmd:MD_DataIdentification">
    <xsl:if test="gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString">
    <dc:title><xsl:value-of select="gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString"/></dc:title>
    </xsl:if>
    
    <xsl:if test="gmd:abstract/gco:CharacterString">
    <dct:abstract><xsl:value-of select="gmd:abstract/gco:CharacterString"/></dct:abstract>
    </xsl:if>    

    <xsl:for-each select="gmd:descriptiveKeywords/gmd:MD_Keywords/gmd:keyword">
      <dc:subject><xsl:value-of select="gco:CharacterString"/></dc:subject>
    </xsl:for-each>

    <xsl:if test="gmd:resourceConstraints/gmd:MD_LegalConstraints/gmd:useLimitation/gco:CharacterString">
       <dc:rights><xsl:value-of select="gmd:resourceConstraints/gmd:MD_LegalConstraints/gmd:useLimitation/gco:CharacterString"/></dc:rights>
    </xsl:if>
    
    <xsl:if test="gmd:language/gco:CharacterString">
       <dc:language><xsl:value-of select="gmd:language/gco:CharacterString"/></dc:language>
    </xsl:if>   

  </xsl:template> 

  <!-- dates relevant to publication and processing -->
  <xsl:template match="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation">
  <xsl:for-each select="gmd:date">
  <!--<dc:FOO/>-->
  <xsl:apply-templates select="gmd:CI_Date"/> 
  </xsl:for-each>
  </xsl:template>

  <!-- each date has an attribute giving its type. how to process? 
       types seen in sample are: publication, revision, creation
    <gmd:date>
         <gmd:CI_Date>
              <gmd:date>
                <gco:DateTime>2008-06-30T13:40:13.835+00:00</gco:DateTime>
              </gmd:date>
              <gmd:dateType>
              <gmd:CI_DateTypeCode codeListValue="publication" codeList="http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode"/>
              </gmd:dateType>
         </gmd:CI_Date>
    </gmd:date>
   -->     
  <xsl:template match="gmd:CI_Date">
    <xsl:variable name="dateType" select="gmd:dateType/gmd:CI_DateTypeCode/@codeListValue"/>
    <xsl:variable name="date" select="gmd:date/gco:DateTime"/>

    <xsl:if test="contains('publication',$dateType)">
      <xsl:element name="published" namespace="http://purl.org/dc/terms/"><xsl:value-of select="$date"/></xsl:element> 
    </xsl:if>

    <xsl:if test="contains('revision',$dateType)">
      <xsl:element name="modified" namespace="http://purl.org/dc/terms/"><xsl:value-of select="$date"/></xsl:element>
    </xsl:if>

    <xsl:if test="contains($dateType,'creation')">
      <xsl:element name="created" namespace="http://purl.org/dc/terms/"><xsl:value-of select="$date"/></xsl:element>
    </xsl:if>
  
  </xsl:template>
 
  <!-- spatial extents --> 
  <xsl:template match="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:geographicElement/gmd:EX_GeographicBoundingBox">
    <xsl:variable name="south" select="gmd:southBoundLatitude/gco:Decimal" />
    <xsl:variable name="north" select="gmd:northBoundLatitude/gco:Decimal" />
    <xsl:variable name="west" select="gmd:westBoundLongitude/gco:Decimal" />
    <xsl:variable name="east" select="gmd:eastBoundLongitude/gco:Decimal" />
    <xsl:text>POLYGON ((</xsl:text>
    <xsl:value-of select="$west" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$south" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$west" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$north" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$east" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$north" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$east" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$south" />
    <xsl:text>, </xsl:text>
    <xsl:value-of select="$west" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="$south" />
    <xsl:text>))</xsl:text>
  </xsl:template>

  <!-- temporal extents -->
  <xsl:template match="gmd:identificationInfo/gmd:MD_DataIdentification/gmd:extent/gmd:EX_Extent/gmd:temporalElement/gmd:EX_TemporalExtent/gmd:extent/gml:TimePeriod">
    <ical:dtstart>
    <xsl:value-of select="gml:beginPosition"/>
    </ical:dtstart>
    <ical:dtend>
    <xsl:value-of select="gml:endPosition"/>
    </ical:dtend>
  </xsl:template>

  <xsl:template match="gmd:distributionInfo/gmd:MD_Distribution">      
  <!-- file and service distribution info -->
  <xsl:for-each select="gmd:transferOptions/gmd:MD_DigitalTransferOptions/gmd:onLine/gmd:CI_OnlineResource">

   <dclite4g:onlineResource>
        <xsl:variable name="protocol" select="gmd:protocol/gco:CharacterString"/>
        <xsl:element name="{$protocol}" namespace="http://dclite4g.xmlns.com/ws.rdf#"> 
        <xsl:attribute name="rdf:about">
          <xsl:value-of select="gmd:linkage/gmd:URL" />
        </xsl:attribute>     
        <gmd:protocol><xsl:value-of select="gmd:protocol/gco:CharacterString"/></gmd:protocol>
        </xsl:element>
        <!--<dclite4g:series>
          <xsl:attribute name="rdf:resource">
            <xsl:value-of select="concat('#',/gmd:MD_Metadata/gmd:fileIdentifier)" />
          </xsl:attribute>
        </dclite4g:series>-->

     </dclite4g:onlineResource>
  </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
